/**
 * 
 */
package fr.sportcom.sportcom.model;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


import android.os.Parcel;
import android.os.Parcelable;


/**
 * @author NeoXy
 *
 */
public class User implements Parcelable {

	private static final DateFormat dFm = new SimpleDateFormat("yyyy-MM-dd", Locale.FRANCE);

	private int id;
	private String mail;
	private String firstName;
	private String lastName;
	private Date born = null;
	private String city = "";
	private boolean owner = false;
	private boolean accepted = true;

	public User(int id, String mail, String firstName, String lastName) {
		this.id = id;
		this.mail = mail;
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public User(int id, String mail, String firstName, String lastName,
			String born, String city) {
		this(id, mail, firstName, lastName);
		this.setBorn(born);
		this.city = city;
	}

	public User(int id, String mail, String firstName, String lastName,
			String born, String city, boolean owner) {
		this(id, mail, firstName, lastName, born, city);
		this.owner = owner;
	}

	public User(int id, String mail, String firstName, String lastName,
			String born, String city, int owner) {
		this(id, mail, firstName, lastName, born, city);
		this.setOwner(owner);
	}

	public User(int id, String mail, String firstName, String lastName,
			String born, String city, int owner, int accepted) {
		this(id, mail, firstName, lastName, born, city, owner);
		this.setAccepted(accepted);
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the mail
	 */
	public String getMail() {
		return mail;
	}

	/**
	 * @param mail the mail to set
	 */
	public void setMail(String mail) {
		this.mail = mail;
	}

	/**
	 * @return the firstName
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * @param firstName the firstName to set
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * @return the lastName
	 */
	public String getLastName() {
		return lastName;
	}

	/**
	 * @param lastName the lastName to set
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	/**
	 * @return the age
	 */
	public int getAge() {
		Calendar curr = Calendar.getInstance();
		Calendar birth = Calendar.getInstance();
		birth.setTime(this.born);
		int yeardiff = curr.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
		curr.add(Calendar.YEAR,-yeardiff);
		if(birth.after(curr))
		{
			yeardiff = yeardiff - 1;
		}
		return yeardiff;
	}

	/**
	 * @return the city
	 */
	public String getCity() {
		return city;
	}

	/**
	 * @param city the city to set
	 */
	public void setCity(String city) {
		this.city = city;
	}

	/**
	 * @return the owner
	 */
	public boolean isOwner() {
		return owner;
	}

	/**
	 * @param owner the owner to set
	 */
	public void setOwner(boolean owner) {
		this.owner = owner;
	}

	/**
	 * @param accepted the accepted to set
	 */
	public void setOwner(int owner) {
		this.owner = (owner!=0);
	}

	/**
	 * @return the accepted
	 */
	public boolean isAccepted() {
		return accepted;
	}

	/**
	 * @param accepted the accepted to set
	 */
	public void setAccepted(boolean accepted) {
		this.accepted = accepted;
	}

	/**
	 * @param accepted the accepted to set
	 */
	public void setAccepted(int accepted) {
		this.accepted = (accepted!=0);
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {	
		dest.writeInt(this.id);
		dest.writeString(this.mail);
		dest.writeString(this.firstName);
		dest.writeString(this.lastName);
		dest.writeString(dFm.format(this.born));
		dest.writeString(this.city);
		dest.writeInt(this.owner ? 1 : 0);
	}

	/**
	 * @return the born
	 */
	public Date getBorn() {
		return born;
	}
	
	public void getBorn(int year, int month, int day) {
		DateFormat format = new SimpleDateFormat("yyyy", Locale.FRANCE);
		year =  Integer.parseInt(format.format(born));
		format = new SimpleDateFormat("MM", Locale.FRANCE);
		month =  Integer.parseInt(format.format(born));
		format = new SimpleDateFormat("dd", Locale.FRANCE);
		day =  Integer.parseInt(format.format(born));
	}
	
	public String getStrBorn() {
		return dFm.format(born);
	}

	/**
	 * @param born the born to set
	 */
	public void setBorn(Date born) {
		this.born = born;
	}

	/**
	 * @param born the born to set
	 */
	public void setBorn(String born) {
		try {
			this.born = dFm.parse(born);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	public static String hashSha256(String pass) {
		MessageDigest md = null;
		String text = pass;

		try {
			md = MessageDigest.getInstance("SHA-256");
			md.update(text.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e1) {
			e1.printStackTrace();
			return "";
		} catch (UnsupportedEncodingException e2) {
			e2.printStackTrace();
			return "";
		}
		byte[] digest = md.digest();

		return byteToHex(digest);
	}

	private static String byteToHex(byte[] bits) {
		if (bits == null) {
			return null;
		}
		StringBuffer hex = new StringBuffer(bits.length * 2); // encod(1_bit) => 2 digits
		for (int i = 0; i < bits.length; i++) {
			if (((int) bits[i] & 0xff) < 0x10) { // 0 < .. < 9
				hex.append("0");
			}
			hex.append(Integer.toString((int) bits[i] & 0xff, 16)); // [(bit+256)%256]^16
		}
		return hex.toString();
	}

	public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
		@Override
		public User createFromParcel(Parcel source)
		{
			return new User(source);
		}

		@Override
		public User[] newArray(int size)
		{
			return new User[size];
		}
	};

	public User(Parcel in) {
		this(in.readInt(),
				in.readString(),
				in.readString(),
				in.readString(),
				in.readString(),
				in.readString(),
				in.readInt()
				);
	}

	public boolean isCreator(CEvent event) {
		return this.id == event.getIdOwner();
	}

}
